<template>
	<om-formsub-outer :index="index" @save="save" @delete="del">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, formValidation, changeEditor, configSelectData} from '../../../static/js/formUtil.js';
	import {getDateStr} from '../../../static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				formdata:{},
				initValue:{
					yearOrderNum3:"",
					bearUser3Id:'',
					bearUser3Name:''
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				workName3Data:[],
				year:'',
				monthNum:'',
				workPlanCode:'DD021014001',
				bearOff3CacheKey:'obj-m-instwork-bearoff3-cache',
				evaOff3CacheKey:'obj-m-instwork-evaoff3-cache',
				workPlanDay:"",
				workName3CacheKey:'obj-m-instwork-workname3-cache',
			}
		},
		methods: {
			save(){
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if (!checkRes) {
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					}else{
						this.datas.push(this.formdata)
					}
					uni.setStorage({
					  key: this.cachename,
					  data: this.datas,
					})
					uni.navigateBack()
				} else {
					_that.om.toast(checkRes);
				}
			},
			del() {
				if(this.index != null) {
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
					 key: this.cachename,
					 data: this.datas
				})
				uni.navigateBack()
			},
			onSelectWorkItem(data){
				this.$refs.autoform.formdata.workName3 = data.workName3 
				this.$refs.autoform.formdata.yearOrderNum3 = data.yearOrderNum3 
				this.$refs.autoform.formdata.workOutput3 = data.workOutput3
				this.$refs.autoform.formdata.deliveryName3 = data.deliveryName3
				this.$refs.autoform.formdata.isimportantItem3Code = data.isimportantItem3Code
				this.$refs.autoform.formdata.isimportantItem3Name = data.isimportantItem3Name
			},
			onSelectBearUser(item){
				this.$refs.autoform.formdata.bearUser3Id = item.userId
				this.$refs.autoform.formdata.bearUser3Name = item.userName
			},
			verifyPlanEnd3(date){
				let _that = this
				var d1 = new Date(_that.year, _that.monthNum, 0);
				var d = new Date(date);
				if(_that.workPlanDay && d1 && d && (d1.getDate() - d.getDate()) < _that.workPlanDay){
					return '管控参数约束“计划完成日期”可选的最大天数为月末倒数第' + (_that.workPlanDay + 1) + '天，请重新选择！';
				}
				return true
			},
			onSelectBearOff3Id(userId) {
				var editor = this.$refs.autoform.getEditor("workInfo","evaOff3Id");
				if(editor && editor.linkParam) {
					editor.linkParam.other = JSON.stringify({
						noUserIdStr:userId
					})
				}
			},
			onSelectEvaOff3Id(userId) {
				var editor = this.$refs.autoform.getEditor("workInfo","bearOff3Id");
				if(editor && editor.linkParam) {
					editor.linkParam.other = JSON.stringify({
						noUserIdStr:userId
					})
				}
			},
			initConfig(){
				let _that = this
				var pro1 = _that.om.request('tinstMonthlyTargetWork/getWorkList');
				Promise.all([pro1]).then(
					(data) => {
						if(_that.$refs.autoform){
							var d = new Date(_that.year, _that.monthNum - 1, 1);
							var d1 = new Date(_that.year, _that.monthNum, 0);
							_that.$refs.autoform.changeEditor(_that.autoformdata[0].key,"planEnd3","min", getDateStr(d))
							_that.$refs.autoform.changeEditor(_that.autoformdata[0].key,"planEnd3","max", getDateStr(d1))
							// _that.$refs.autoform.changeEditor(_that.autoformdata[0].key,"workName3","data", data[0].map(e=>e.workName3))
							// _that.$refs.autoform.changeEditor(_that.autoformdata[0].key,"workName3","data", data[0].map(e=>e.workName3))
							_that.$refs.autoform.changeEditor(_that.autoformdata[0].key, "workName3", "linkParam", {
								namefield:"workName3|orderNum3",
								notefield:"workOutput3",
								extrafield:"planEnd3",
								likefield:"workName3",
								initload:true,
								url:"tinstMonthlyTargetWork/getWorkList",
								cachename:_that.workName3CacheKey,
								selectTitle:"年度工作计划"
							})
							// _that.$refs.autoform.changeEditor(_that.autoformdata[0].key,"bearOff3Id","data", configSelectData(data[1],"id","userName"))
							// _that.$refs.autoform.initSelectIndex(_that.autoformdata[0].key, "bearOff3Id")
							_that.$refs.autoform.changeEditor(_that.autoformdata[0].key, "bearOff3Id", "link","../../public/search/search")
							_that.$refs.autoform.changeEditor(_that.autoformdata[0].key, "bearOff3Id", "linkParam", {
								namefield:"userName",
								notefield:"posName",
								extrafield:"instShortName",
								searchfield:"q",
								initload:true,
								url:"orgInstPosUserOfficial/findListByUserIdOrgId",
								cachename:_that.bearOff3CacheKey,
								placeholder:"请输入姓名或岗位"
							})
							_that.$refs.autoform.changeEditor(_that.autoformdata[0].key,"evaOff3Id","link","../../public/pageSearch/pageSearch")
							_that.$refs.autoform.changeEditor(_that.autoformdata[0].key, "evaOff3Id", "linkParam", {
								valuefield:"id",
								namefield:"userName",
								notefield:"posName",
								extrafield:"instShortName",
								searchfield:"q",
								// initload:true,
								url:"orgInstPosUserOfficial/findPagerByOrgId",
								cachename:_that.evaOff3CacheKey,
								placeholder:"请输入姓名或岗位"
							})
						}
					}
				)
			}
		},
		onLoad(e){
			let _that = this
			_that.om.request('dataDict/getIntByCode',{code:_that.workPlanCode}).then(res=>{
				if(res.succeed){
					 _that.workPlanDay = res.value
				}
			})
			subOnLoad(_that, e, function() {
				_that.year = e.year
				_that.monthNum = e.monthNum
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.initConfig()
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: this.evaOff3CacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.evaOff3Id = d.id
							formdata.evaOff3Name = d.userName
							formdata.evaUser3Id = d.userId
							formdata.evaUser3Name = d.userName
							uni.removeStorage({
								key: _that.evaOff3CacheKey
							});
							_that.onSelectEvaOff3Id(d.userId)
						}
					}
				});
				uni.getStorage({
					key: this.bearOff3CacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.bearOff3Id = d.id
							formdata.bearOff3Name = d.userName
							formdata.bearUser3Id = d.userId
							formdata.bearUser3Name = d.userName
							uni.removeStorage({
								key: _that.bearOff3CacheKey
							});
							_that.onSelectBearOff3Id(d.userId)
						}
					}
				});
				uni.getStorage({
					key: this.workName3CacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							_that.onSelectWorkItem(d)
							uni.removeStorage({
								key: _that.workName3CacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
